﻿addLoadEvent(bulidListAction);

function bulidListAction(){
     var pageList=$("pagelist");
     if(pageList==null){
        pageList=$("booklist");
     }
     if(pageList==null){
        return;
     }
     var itemlist=$as.call(pageList,"li");
     var len=itemlist.length;
     for(var i=0;i<len;i++){
        bindLiEvent.call(itemlist[i])
        var alist=$as.call(itemlist[i],"a");
        for(var j=0;j<alist.length;j++){
            var abj=alist[j];
            var href=abj.getAttribute("href");
            if(href.indexOf("updatePage.xhtml?Pid")>-1){
                 bindSaveEvent.call(abj,href,itemlist[i]);
            }
             if(href.indexOf("Delete.xhtml")>-1){
               bindDelEvent.call(abj,href,itemlist[i]);
               continue;
            }
        }
    }
}
function bindLiEvent(){
    var ins=$a.call(this,"ins");
    if(ins==null)return;
    this.onmouseover=function(){
        this.style.background="#ff99cc"//"#f2f2f2";
        ins.filters.alpha.opacity=100;
    }
    this.onmouseout=function(){
        this.style.background="#f2f2f2";
        ins.filters.alpha.opacity=80;
    }
    
    
}
function bindSaveEvent(href,liobj){
   this.areas=$as.call(liobj,"textarea");
   for(var i=0;i<this.areas.length;i++){
        bindTextAreaEvent.call(this.areas[i]);
   }
    this.onclick=function(){
        this.index=this.areas[0].value;
        this.title=this.areas[1].value;
        
        liobj.refBtn=this;
        this.hrefString=href;
        this.postString="index="+this.index+"&title="+this.title;
        var Total = new ajax.$x(this.hrefString,"POST",returnUpdateFun,errorFun,this.postString,liobj,false);
        this.style.display="none";
        return false;
    }
}
function bindTextAreaEvent(){
    this.onfocus=function(){
        focusMe(this);
    }
    this.onblur=function(){
        blurMe(this);
    }
    this.onmouseover=function(){
        overMe(this);
    }
    this.onmouseout=function(){
        outMe(this);
    }
}
function returnUpdateFun(){
    var res=this.req.responseText;
    var notice=document.createElement("div");
    notice.setAttribute("class","notice");
    notice.setAttribute("className","notice");//for ie;
    var noticeText=document.createTextNode(res);
    notice.appendChild(noticeText);
    this.relate.appendChild(notice);
    setTimeout(clearNotice,1000,this.relate,notice);
}
function clearNotice(parent,child){
    parent.refBtn.style.display="";
    parent.removeChild(child);
    //重新排序;
    /*var ulRef=parent.parentNode;
    var itemlist=$as.call(ulRef,"li");//ulRef.getElementsByTagName("li");
    
    var len=itemlist.length;
    var liArray=new Array();
    for(var i=0;i<len;i++){
        var indexArea=$a.call(itemlist[i],"textarea");//itemlist[i].getElementsByTagName("textarea")[0];
        var liRef=new listRefObj(itemlist[i],indexArea.value);
        liArray.push(liRef);
    }
    liArray.sort(sortFun);
    
    ulRef.innerHTML="";
    for(var i=0;i<len;i++){
        ulRef.appendChild(liArray[i].li);
    }
    //重新增加事件;
    bulidListAction();
    initsimplebox();*/
}

//用于li对像排序;
var listRefObj=function(liobj,index){
    this.li=liobj.cloneNode(true);
    this.index=index;
}

function sortFun(a,b){
    return a.index-b.index;
}



//创建删除提示,用ajax处理;
function bindDelEvent(href,liobj){
    this.onclick=function(){
        if(confirm("确定要删除")){
        href+="&ok=1";
         var Total = new ajax.$x(href,"GET",returnDelFun,null,"",liobj,false);
        }
        return false;
    }
}

function returnDelFun(){
    var res = this.req.responseText;
   // alert(this.relate);//parentNode)
    this.relate.parentNode.removeChild(this.relate)
}
function errorFun(){
    alert("error");
}

//鼠标事件;
function focusMe(o){
 	 o.inFocus=true;
 	 o.style.border="1px solid #AAC1E6";
 	 o.style.backgroundColor="#fff";
 	 o.style.cursor="text";
 	 o.select();
	 reHeight(o);

 }
  function reHeight(o){
  	// if (o.inFocus) {o.style.height = "40px";}
 }
 
 function blurMe(o){
 	 o.inFocus=false;
	 o.style.border="0px #fff solid"
 	 o.style.padding="1px 3px";
 	 o.style.backgroundColor="transparent";
 	 o.style.cursor="pointer";
 	 o.style.overflowY="hidden";
 	 reHeight(o);
 }
 function overMe(o){
 	 if (!o.inFocus) {
 	 	 o.style.border="1px solid #AAC1E6";
 	 	 o.style.padding="0px 3px";
 	 	 o.style.backgroundColor="#F3F8FF";
 	 	 reHeight(o);
 	 }
 }
 
 function outMe(o){
 	 if (!o.inFocus) {
	 	 o.style.border="0px #fff solid"
 	 	 o.style.padding="1px 3px";
 	 	 o.style.backgroundColor="transparent";
  	 }
 }